
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>附录C 消息传递框架与完整的ATM示例 · C++并发编程实战（第2版）</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="陈晓伟 chenxiaowei">
        
        
    
    <link rel="stylesheet" href="../../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="../appendix_D/D.0-chinese.html" />
    
    
    <link rel="prev" href="../appendix_B/B.0-chinese.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../../">
            
                <a href="../../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../Praise_for_the_first_edition.html">
            
                <a href="../Praise_for_the_first_edition.html">
            
                    
                    第一版的赞许
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../preface-chinese.html">
            
                <a href="../preface-chinese.html">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../acknowledgments-chinese.html">
            
                <a href="../acknowledgments-chinese.html">
            
                    
                    感谢
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../about_this_book-chinese.html">
            
                <a href="../about_this_book-chinese.html">
            
                    
                    关于本书
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../about_the_author-chinese.html">
            
                <a href="../about_the_author-chinese.html">
            
                    
                    关于作者
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../about_cover_illustration-chinese.html">
            
                <a href="../about_cover_illustration-chinese.html">
            
                    
                    关于封面
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter1/1.0-chinese.html">
            
                <a href="../chapter1/1.0-chinese.html">
            
                    
                    第1章 你好，并发世界
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter1/1.1-chinese.html">
            
                <a href="../chapter1/1.1-chinese.html">
            
                    
                    1.1 何谓并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter1/1.2-chinese.html">
            
                <a href="../chapter1/1.2-chinese.html">
            
                    
                    1.2 为什么使用并发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter1/1.3-chinese.html">
            
                <a href="../chapter1/1.3-chinese.html">
            
                    
                    1.3 并发和多线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter1/1.4-chinese.html">
            
                <a href="../chapter1/1.4-chinese.html">
            
                    
                    1.4 开始入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter1/1.5-chinese.html">
            
                <a href="../chapter1/1.5-chinese.html">
            
                    
                    1.5 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter2/2.0-chinese.html">
            
                <a href="../chapter2/2.0-chinese.html">
            
                    
                    第2章 线程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter2/2.1-chinese.html">
            
                <a href="../chapter2/2.1-chinese.html">
            
                    
                    2.1 线程的基本操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter2/2.2-chinese.html">
            
                <a href="../chapter2/2.2-chinese.html">
            
                    
                    2.2 传递参数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter2/2.3-chinese.html">
            
                <a href="../chapter2/2.3-chinese.html">
            
                    
                    2.3 转移所有权
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter2/2.4-chinese.html">
            
                <a href="../chapter2/2.4-chinese.html">
            
                    
                    2.4 确定线程数量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter2/2.5-chinese.html">
            
                <a href="../chapter2/2.5-chinese.html">
            
                    
                    2.5 线程标识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.6" data-path="../chapter2/2.6-chinese.html">
            
                <a href="../chapter2/2.6-chinese.html">
            
                    
                    2.6 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="../chapter3/3.0-chinese.html">
            
                <a href="../chapter3/3.0-chinese.html">
            
                    
                    第3章 共享数据
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.10.1" data-path="../chapter3/3.1-chinese.html">
            
                <a href="../chapter3/3.1-chinese.html">
            
                    
                    3.1 共享数据的问题
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="../chapter3/3.2-chinese.html">
            
                <a href="../chapter3/3.2-chinese.html">
            
                    
                    3.2 使用互斥量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="../chapter3/3.3-chinese.html">
            
                <a href="../chapter3/3.3-chinese.html">
            
                    
                    3.3 保护共享数据的方式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="../chapter3/3.4-chinese.html">
            
                <a href="../chapter3/3.4-chinese.html">
            
                    
                    3.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter4/4.0-chinese.html">
            
                <a href="../chapter4/4.0-chinese.html">
            
                    
                    第4章 同步操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter4/4.1-chinese.html">
            
                <a href="../chapter4/4.1-chinese.html">
            
                    
                    4.1 等待事件或条件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter4/4.2-chinese.html">
            
                <a href="../chapter4/4.2-chinese.html">
            
                    
                    4.2 使用future
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter4/4.3-chinese.html">
            
                <a href="../chapter4/4.3-chinese.html">
            
                    
                    4.3 限时等待
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter4/4.4-chinese.html">
            
                <a href="../chapter4/4.4-chinese.html">
            
                    
                    4.4 简化代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter4/4.5-chinese.html">
            
                <a href="../chapter4/4.5-chinese.html">
            
                    
                    4.5 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter5/5.0-chinese.html">
            
                <a href="../chapter5/5.0-chinese.html">
            
                    
                    第5章 内存模型和原子操作
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.12.1" data-path="../chapter5/5.1-chinese.html">
            
                <a href="../chapter5/5.1-chinese.html">
            
                    
                    5.1 内存模型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.2" data-path="../chapter5/5.2-chinese.html">
            
                <a href="../chapter5/5.2-chinese.html">
            
                    
                    5.2 原子操作和原子类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.3" data-path="../chapter5/5.3-chinese.html">
            
                <a href="../chapter5/5.3-chinese.html">
            
                    
                    5.3 同步操作和强制排序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12.4" data-path="../chapter5/5.4-chinese.html">
            
                <a href="../chapter5/5.4-chinese.html">
            
                    
                    5.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../chapter6/6.0-chinese.html">
            
                <a href="../chapter6/6.0-chinese.html">
            
                    
                    第6章 设计基于锁的并发数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../chapter6/6.1-chinese.html">
            
                <a href="../chapter6/6.1-chinese.html">
            
                    
                    6.1 并发设计的意义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../chapter6/6.2-chinese.html">
            
                <a href="../chapter6/6.2-chinese.html">
            
                    
                    6.2 基于锁的并发数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../chapter6/6.3-chinese.html">
            
                <a href="../chapter6/6.3-chinese.html">
            
                    
                    6.3 设计更加复杂的数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../chapter6/6.4-chinese.html">
            
                <a href="../chapter6/6.4-chinese.html">
            
                    
                    6.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../chapter7/7.0-chinese.html">
            
                <a href="../chapter7/7.0-chinese.html">
            
                    
                    第7章 设计无锁的并发数据结构
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../chapter7/7.1-chinese.html">
            
                <a href="../chapter7/7.1-chinese.html">
            
                    
                    7.1 定义和意义
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../chapter7/7.2-chinese.html">
            
                <a href="../chapter7/7.2-chinese.html">
            
                    
                    7.2 无锁数据结构的例子
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.3" data-path="../chapter7/7.3-chinese.html">
            
                <a href="../chapter7/7.3-chinese.html">
            
                    
                    7.3 设计无锁数据结构的指导建议
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.4" data-path="../chapter7/7.4-chinese.html">
            
                <a href="../chapter7/7.4-chinese.html">
            
                    
                    7.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="../chapter8/8.0-chinese.html">
            
                <a href="../chapter8/8.0-chinese.html">
            
                    
                    第8章 并发设计
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.15.1" data-path="../chapter8/8.1-chinese.html">
            
                <a href="../chapter8/8.1-chinese.html">
            
                    
                    8.1 线程间划分工作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.2" data-path="../chapter8/8.2-chinese.html">
            
                <a href="../chapter8/8.2-chinese.html">
            
                    
                    8.2 并发代码的性能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.3" data-path="../chapter8/8.3-chinese.html">
            
                <a href="../chapter8/8.3-chinese.html">
            
                    
                    8.3 为多线程性能设计数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.4" data-path="../chapter8/8.4-chinese.html">
            
                <a href="../chapter8/8.4-chinese.html">
            
                    
                    8.4 设计并发代码的注意事项
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.5" data-path="../chapter8/8.5-chinese.html">
            
                <a href="../chapter8/8.5-chinese.html">
            
                    
                    8.5 在实践中设计并发代码
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15.6" data-path="../chapter8/8.6-chinese.html">
            
                <a href="../chapter8/8.6-chinese.html">
            
                    
                    8.6 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="../chapter9/9.0-chinese.html">
            
                <a href="../chapter9/9.0-chinese.html">
            
                    
                    第9章 高级线程管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.16.1" data-path="../chapter9/9.1-chinese.html">
            
                <a href="../chapter9/9.1-chinese.html">
            
                    
                    9.1 线程池
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.2" data-path="../chapter9/9.2-chinese.html">
            
                <a href="../chapter9/9.2-chinese.html">
            
                    
                    9.2 中断线程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16.3" data-path="../chapter9/9.3-chinese.html">
            
                <a href="../chapter9/9.3-chinese.html">
            
                    
                    9.3 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="../chapter10/10.0-chinese.html">
            
                <a href="../chapter10/10.0-chinese.html">
            
                    
                    第10章 并行算法
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.17.1" data-path="../chapter10/10.1-chinese.html">
            
                <a href="../chapter10/10.1-chinese.html">
            
                    
                    10.1 并行化标准库算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.2" data-path="../chapter10/10.2-chinese.html">
            
                <a href="../chapter10/10.2-chinese.html">
            
                    
                    10.2 执行策略
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.3" data-path="../chapter10/10.3-chinese.html">
            
                <a href="../chapter10/10.3-chinese.html">
            
                    
                    10.3 C++标准库中的并行算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17.4" data-path="../chapter10/10.4-chinese.html">
            
                <a href="../chapter10/10.4-chinese.html">
            
                    
                    10.4 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="../chapter11/11.0-chinese.html">
            
                <a href="../chapter11/11.0-chinese.html">
            
                    
                    第11章 测试和调试多线程应用
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.18.1" data-path="../chapter11/11.1-chinese.html">
            
                <a href="../chapter11/11.1-chinese.html">
            
                    
                    11.1 与并发相关的Bug
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.2" data-path="../chapter11/11.2-chinese.html">
            
                <a href="../chapter11/11.2-chinese.html">
            
                    
                    11.2 定位并发Bug的技巧
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18.3" data-path="../chapter11/11.3-chinese.html">
            
                <a href="../chapter11/11.3-chinese.html">
            
                    
                    11.3 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="../appendix_A/A.0-chinese.html">
            
                <a href="../appendix_A/A.0-chinese.html">
            
                    
                    附录A C++11特性简明参考(部分)
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.19.1" data-path="../appendix_A/A.1-chinese.html">
            
                <a href="../appendix_A/A.1-chinese.html">
            
                    
                    A.1 右值引用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.2" data-path="../appendix_A/A.2-chinese.html">
            
                <a href="../appendix_A/A.2-chinese.html">
            
                    
                    A.2 删除函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.3" data-path="../appendix_A/A.3-chinese.html">
            
                <a href="../appendix_A/A.3-chinese.html">
            
                    
                    A.3 默认函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.4" data-path="../appendix_A/A.4-chinese.html">
            
                <a href="../appendix_A/A.4-chinese.html">
            
                    
                    A.4 常量表达式函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.5" data-path="../appendix_A/A.5-chinese.html">
            
                <a href="../appendix_A/A.5-chinese.html">
            
                    
                    A.5 Lambda函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.6" data-path="../appendix_A/A.6-chinese.html">
            
                <a href="../appendix_A/A.6-chinese.html">
            
                    
                    A.6 变参模板
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.7" data-path="../appendix_A/A.7-chinese.html">
            
                <a href="../appendix_A/A.7-chinese.html">
            
                    
                    A.7 自动推导变量类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.8" data-path="../appendix_A/A.8-chinese.html">
            
                <a href="../appendix_A/A.8-chinese.html">
            
                    
                    A.8 线程本地变量
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.9" data-path="../appendix_A/A.9-chinese.html">
            
                <a href="../appendix_A/A.9-chinese.html">
            
                    
                    A.9 模板类参数的推导
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19.10" data-path="../appendix_A/A.10-chinese.html">
            
                <a href="../appendix_A/A.10-chinese.html">
            
                    
                    A.10 本章总结
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="../appendix_B/B.0-chinese.html">
            
                <a href="../appendix_B/B.0-chinese.html">
            
                    
                    附录B 并发库简要对比
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.21" data-path="C.0-chinese.html">
            
                <a href="C.0-chinese.html">
            
                    
                    附录C 消息传递框架与完整的ATM示例
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="../appendix_D/D.0-chinese.html">
            
                <a href="../appendix_D/D.0-chinese.html">
            
                    
                    附录D C++线程类库参考
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.22.1" data-path="../appendix_D/D.1-chinese.html">
            
                <a href="../appendix_D/D.1-chinese.html">
            
                    
                    D.1 chrono头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.2" data-path="../appendix_D/D.2-chinese.html">
            
                <a href="../appendix_D/D.2-chinese.html">
            
                    
                    D.2 condition_variable头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.3" data-path="../appendix_D/D.3-chinese.html">
            
                <a href="../appendix_D/D.3-chinese.html">
            
                    
                    D.3 atomic头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.4" data-path="../appendix_D/D.4-chinese.html">
            
                <a href="../appendix_D/D.4-chinese.html">
            
                    
                    D.4 future头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.5" data-path="../appendix_D/D.5-chinese.html">
            
                <a href="../appendix_D/D.5-chinese.html">
            
                    
                    D.5 mutex头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.6" data-path="../appendix_D/D.6-chinese.html">
            
                <a href="../appendix_D/D.6-chinese.html">
            
                    
                    D.6 ratio头文件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22.7" data-path="../appendix_D/D.7-chinese.html">
            
                <a href="../appendix_D/D.7-chinese.html">
            
                    
                    D.7 thread头文件
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../.." >附录C 消息传递框架与完整的ATM示例</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6D88;&#x606F;&#x4F20;&#x9012;&#x6846;&#x67B6;&#x4E0E;&#x5B8C;&#x6574;&#x7684;atm&#x793A;&#x4F8B;">&#x6D88;&#x606F;&#x4F20;&#x9012;&#x6846;&#x67B6;&#x4E0E;&#x5B8C;&#x6574;&#x7684;ATM&#x793A;&#x4F8B;</h1>
<p>ATM&#xFF1A;&#x81EA;&#x52A8;&#x53D6;&#x6B3E;&#x673A;&#x3002;</p>
<p>&#x56DE;&#x5230;&#x7B2C;4&#x7AE0;&#xFF0C;&#x6211;&#x4E3E;&#x4E86;&#x4E00;&#x4E2A;&#x4F7F;&#x7528;&#x6D88;&#x606F;&#x4F20;&#x9012;&#x6846;&#x67B6;&#x5728;&#x7EBF;&#x7A0B;&#x95F4;&#x53D1;&#x9001;&#x4FE1;&#x606F;&#x7684;&#x4F8B;&#x5B50;&#x3002;&#x8FD9;&#x91CC;&#x5C31;&#x4F1A;&#x4F7F;&#x7528;&#x8FD9;&#x4E2A;&#x5B9E;&#x73B0;&#x6765;&#x5B8C;&#x6210;ATM&#x529F;&#x80FD;&#x3002;&#x4E0B;&#x9762;&#x5B8C;&#x6574;&#x4EE3;&#x7801;&#x5C31;&#x662F;&#x529F;&#x80FD;&#x7684;&#x5B9E;&#x73B0;&#xFF0C;&#x5305;&#x62EC;&#x6D88;&#x606F;&#x4F20;&#x9012;&#x6846;&#x67B6;&#x3002;</p>
<p>&#x4EE3;&#x7801;C.1&#x5B9E;&#x73B0;&#x4E86;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#x961F;&#x5217;&#xFF0C;&#x53EF;&#x4EE5;&#x5C06;&#x6D88;&#x606F;&#x4EE5;&#x6307;&#x9488;(&#x6307;&#x5411;&#x57FA;&#x7C7B;)&#x7684;&#x65B9;&#x5F0F;&#x5B58;&#x50A8;&#x5728;&#x5217;&#x8868;&#x4E2D;&#xFF0C;&#x6307;&#x5B9A;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x4F1A;&#x7531;&#x57FA;&#x7C7B;&#x6D3E;&#x751F;&#x6A21;&#x677F;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x3002;&#x63A8;&#x9001;&#x5305;&#x88C5;&#x7C7B;&#x7684;&#x6784;&#x9020;&#x5B9E;&#x4F8B;&#xFF0C;&#x4EE5;&#x53CA;&#x5B58;&#x50A8;&#x6307;&#x5411;&#x8FD9;&#x4E2A;&#x5B9E;&#x4F8B;&#x7684;&#x6307;&#x9488;&#xFF0C;&#x5F39;&#x51FA;&#x5B9E;&#x4F8B;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5C06;&#x4F1A;&#x8FD4;&#x56DE;&#x6307;&#x5411;&#x5176;&#x7684;&#x6307;&#x9488;&#x3002;&#x56E0;&#x4E3A;message_base&#x7C7B;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x6210;&#x5458;&#x51FD;&#x6570;&#xFF0C;&#x5728;&#x8BBF;&#x95EE;&#x5B58;&#x50A8;&#x6D88;&#x606F;&#x4E4B;&#x524D;&#xFF0C;&#x5F39;&#x51FA;&#x7EBF;&#x7A0B;&#x5C31;&#x9700;&#x8981;&#x5C06;&#x6307;&#x9488;&#x8F6C;&#x4E3A;<code>wrapped_message&lt;T&gt;</code>&#x6307;&#x9488;&#x3002;</p>
<p>&#x4EE3;&#x7801;C.1 &#x7B80;&#x5355;&#x7684;&#x6D88;&#x606F;&#x961F;&#x5217;</p>
<pre><code class="lang-c++">#include &lt;mutex&gt;
#include &lt;condition_variable&gt;
#include &lt;queue&gt;
#include &lt;memory&gt;

namespace messaging
{
  struct message_base  // &#x961F;&#x5217;&#x9879;&#x7684;&#x57FA;&#x7840;&#x7C7B;
  {
    virtual ~message_base()
    {}
  };

  template&lt;typename Msg&gt;
  struct wrapped_message:  // &#x6BCF;&#x4E2A;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x90FD;&#x9700;&#x8981;&#x7279;&#x5316;
    message_base
  {
    Msg contents;

    explicit wrapped_message(Msg const&amp; contents_):
      contents(contents_)
    {}
  };

  class queue  // &#x6211;&#x4EEC;&#x7684;&#x961F;&#x5217;
  {
    std::mutex m;
    std::condition_variable c;
    std::queue&lt;std::shared_ptr&lt;message_base&gt; &gt; q;  // &#x5B9E;&#x9645;&#x5B58;&#x50A8;&#x6307;&#x5411;message_base&#x7C7B;&#x6307;&#x9488;&#x7684;&#x961F;&#x5217;
  public:
    template&lt;typename T&gt;
    void push(T const&amp; msg)
    {
      std::lock_guard&lt;std::mutex&gt; lk(m);
      q.push(std::make_shared&lt;wrapped_message&lt;T&gt; &gt;(msg));  // &#x5305;&#x88C5;&#x5DF2;&#x4F20;&#x9012;&#x7684;&#x4FE1;&#x606F;&#xFF0C;&#x5B58;&#x50A8;&#x6307;&#x9488;
      c.notify_all();
    }

    std::shared_ptr&lt;message_base&gt; wait_and_pop()
    {
      std::unique_lock&lt;std::mutex&gt; lk(m);
      c.wait(lk,[&amp;]{return !q.empty();});  // &#x5F53;&#x961F;&#x5217;&#x4E3A;&#x7A7A;&#x65F6;&#x963B;&#x585E;
      auto res=q.front();
      q.pop();
      return res;
    }
  };
}
</code></pre>
<p>&#x53D1;&#x9001;&#x901A;&#x8FC7;sender&#x7C7B;(&#x89C1;&#x4EE3;&#x7801;C.2)&#x5B9E;&#x4F8B;&#x5904;&#x7406;&#x8FC7;&#x7684;&#x6D88;&#x606F;&#x3002;&#x53EA;&#x80FD;&#x5BF9;&#x5DF2;&#x63A8;&#x9001;&#x5230;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x6D88;&#x606F;&#x8FDB;&#x884C;&#x5305;&#x88C5;&#x3002;&#x5BF9;sender&#x5B9E;&#x4F8B;&#x7684;&#x62F7;&#x8D1D;&#xFF0C;&#x53EA;&#x662F;&#x62F7;&#x8D1D;&#x4E86;&#x6307;&#x5411;&#x961F;&#x5217;&#x7684;&#x6307;&#x9488;&#xFF0C;&#x800C;&#x975E;&#x961F;&#x5217;&#x672C;&#x8EAB;&#x3002;</p>
<p>&#x4EE3;&#x7801;C.2 sender&#x7C7B;</p>
<pre><code class="lang-c++">namespace messaging
{
  class sender
  {
    queue*q;  // sender&#x662F;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#x6307;&#x9488;&#x7684;&#x5305;&#x88C5;&#x7C7B;
  public:
    sender():  // sender&#x65E0;&#x961F;&#x5217;(&#x9ED8;&#x8BA4;&#x6784;&#x9020;&#x51FD;&#x6570;)
      q(nullptr)
    {}

    explicit sender(queue*q_):  // &#x4ECE;&#x6307;&#x5411;&#x961F;&#x5217;&#x7684;&#x6307;&#x9488;&#x8FDB;&#x884C;&#x6784;&#x9020;
      q(q_)
    {}

    template&lt;typename Message&gt;
    void send(Message const&amp; msg)
    {
      if(q)
      {
        q-&gt;push(msg);  // &#x5C06;&#x53D1;&#x9001;&#x4FE1;&#x606F;&#x63A8;&#x9001;&#x7ED9;&#x961F;&#x5217;
      }
    }
  };
}
</code></pre>
<p>&#x63A5;&#x6536;&#x4FE1;&#x606F;&#x90E8;&#x5206;&#x6709;&#x4E9B;&#x9EBB;&#x70E6;&#x3002;&#x4E0D;&#x4EC5;&#x8981;&#x7B49;&#x5F85;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x8FD8;&#x8981;&#x68C0;&#x67E5;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x662F;&#x5426;&#x4E0E;&#x6240;&#x7B49;&#x5F85;&#x7684;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x5339;&#x914D;&#xFF0C;&#x5E76;&#x8C03;&#x7528;&#x5904;&#x7406;&#x51FD;&#x6570;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x3002;&#x90A3;&#x4E48;&#x5C31;&#x4ECE;receiver&#x7C7B;&#x7684;&#x5B9E;&#x73B0;&#x5F00;&#x59CB;&#x5427;&#x3002;</p>
<p>&#x4EE3;&#x7801;C.3 receiver&#x7C7B;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">namespace</span> messaging
{
  <span class="hljs-keyword">class</span> receiver
  {
    <span class="hljs-built_in">queue</span> q;  <span class="hljs-comment">// &#x63A5;&#x53D7;&#x8005;&#x62E5;&#x6709;&#x5BF9;&#x5E94;&#x961F;&#x5217;</span>
  <span class="hljs-keyword">public</span>:
    <span class="hljs-function"><span class="hljs-keyword">operator</span> <span class="hljs-title">sender</span><span class="hljs-params">()</span>  <span class="hljs-comment">// &#x5141;&#x8BB8;&#x5C06;&#x7C7B;&#x4E2D;&#x961F;&#x5217;&#x9690;&#x5F0F;&#x8F6C;&#x5316;&#x4E3A;&#x4E00;&#x4E2A;sender&#x961F;&#x5217;</span>
    </span>{
      <span class="hljs-keyword">return</span> sender(&amp;q);
    }
    <span class="hljs-function">dispatcher <span class="hljs-title">wait</span><span class="hljs-params">()</span>  <span class="hljs-comment">// &#x7B49;&#x5F85;&#x5BF9;&#x961F;&#x5217;&#x8FDB;&#x884C;&#x8C03;&#x5EA6;</span>
    </span>{
      <span class="hljs-keyword">return</span> dispatcher(&amp;q);
    }
  };
}
</code></pre>
<p>sender&#x53EA;&#x662F;&#x5F15;&#x7528;&#x4E00;&#x4E2A;&#x6D88;&#x606F;&#x961F;&#x5217;&#xFF0C;&#x800C;receiver&#x662F;&#x62E5;&#x6709;&#x4E00;&#x4E2A;&#x961F;&#x5217;&#x3002;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x9690;&#x5F0F;&#x8F6C;&#x6362;&#x7684;&#x65B9;&#x5F0F;&#x83B7;&#x53D6;sender&#x5F15;&#x7528;&#x7684;&#x7C7B;&#x3002;&#x96BE;&#x70B9;&#x5728;&#x4E8E;wait()&#x4E2D;&#x7684;&#x8C03;&#x5EA6;&#xFF0C;&#x8FD9;&#x91CC;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A;dispatcher&#x5BF9;&#x8C61;&#x5F15;&#x7528;receiver&#x4E2D;&#x7684;&#x961F;&#x5217;&#xFF0C;dispatcher&#x7C7B;&#x5B9E;&#x73B0;&#x4F1A;&#x5728;&#x4E0B;&#x4E00;&#x4E2A;&#x6E05;&#x5355;&#x4E2D;&#x770B;&#x5230;&#x3002;&#x5982;&#x4F60;&#x6240;&#x89C1;&#xFF0C;&#x4EFB;&#x52A1;&#x662F;&#x5728;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4E2D;&#x5B8C;&#x6210;&#x7684;&#x3002;&#x5728;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6240;&#x8981;&#x505A;&#x7684;&#x5DE5;&#x4F5C;&#x662F;&#x5BF9;&#x6D88;&#x606F;&#x8FDB;&#x884C;&#x7B49;&#x5F85;&#xFF0C;&#x4EE5;&#x53CA;&#x5BF9;&#x5176;&#x8FDB;&#x884C;&#x8C03;&#x5EA6;&#x3002;</p>
<p>&#x4EE3;&#x7801;C.4 dispatcher&#x7C7B;</p>
<pre><code class="lang-c++">namespace messaging
{
  class close_queue  // &#x7528;&#x4E8E;&#x5173;&#x95ED;&#x961F;&#x5217;&#x7684;&#x6D88;&#x606F;
  {};

  class dispatcher
  {
    queue* q;
    bool chained;

    dispatcher(dispatcher const&amp;)=delete;  // dispatcher&#x5B9E;&#x4F8B;&#x4E0D;&#x80FD;&#x88AB;&#x62F7;&#x8D1D;
    dispatcher&amp; operator=(dispatcher const&amp;)=delete;

    template&lt;
      typename Dispatcher,
      typename Msg,
      typename Func&gt;  // &#x5141;&#x8BB8;TemplateDispatcher&#x5B9E;&#x4F8B;&#x8BBF;&#x95EE;&#x5185;&#x90E8;&#x6210;&#x5458;
    friend class TemplateDispatcher;

    void wait_and_dispatch()
    {
      for(;;)  // 1 &#x5FAA;&#x73AF;&#xFF0C;&#x7B49;&#x5F85;&#x8C03;&#x5EA6;&#x6D88;&#x606F;
      {
        auto msg=q-&gt;wait_and_pop();
        dispatch(msg);
      }
    }

    bool dispatch(  // 2 dispatch()&#x4F1A;&#x68C0;&#x67E5;close_queue&#x6D88;&#x606F;&#xFF0C;&#x7136;&#x540E;&#x629B;&#x51FA;
      std::shared_ptr&lt;message_base&gt; const&amp; msg)
    {
      if(dynamic_cast&lt;wrapped_message&lt;close_queue&gt;*&gt;(msg.get()))
      {
        throw close_queue();
      }
      return false;
    }
  public:
    dispatcher(dispatcher&amp;&amp; other):  // dispatcher&#x5B9E;&#x4F8B;&#x53EF;&#x4EE5;&#x79FB;&#x52A8;
      q(other.q),chained(other.chained)
    {
      other.chained=true;  // &#x6E90;&#x4E0D;&#x80FD;&#x7B49;&#x5F85;&#x6D88;&#x606F;
    }

    explicit dispatcher(queue* q_):
      q(q_),chained(false)
    {}

    template&lt;typename Message,typename Func&gt;
    TemplateDispatcher&lt;dispatcher,Message,Func&gt;
    handle(Func&amp;&amp; f)  // 3 &#x4F7F;&#x7528;TemplateDispatcher&#x5904;&#x7406;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x7684;&#x6D88;&#x606F;
    {
      return TemplateDispatcher&lt;dispatcher,Message,Func&gt;(
        q,this,std::forward&lt;Func&gt;(f));
    }

    ~dispatcher() noexcept(false)  // 4 &#x6790;&#x6784;&#x51FD;&#x6570;&#x53EF;&#x80FD;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;
    {  
      if(!chained)
      {
        wait_and_dispatch();
      }
    }
  };
}
</code></pre>
<p>&#x4ECE;wait()&#x8FD4;&#x56DE;&#x7684;dispatcher&#x5B9E;&#x4F8B;&#x5C06;&#x9A6C;&#x4E0A;&#x88AB;&#x9500;&#x6BC1;&#xFF0C;&#x56E0;&#x4E3A;&#x662F;&#x4E34;&#x65F6;&#x53D8;&#x91CF;&#xFF0C;&#x4E5F;&#x5982;&#x524D;&#x6587;&#x63D0;&#x5230;&#x7684;&#xFF0C;&#x6790;&#x6784;&#x51FD;&#x6570;&#x5728;&#x8FD9;&#x91CC;&#x505A;&#x771F;&#x6B63;&#x7684;&#x5DE5;&#x4F5C;&#x3002;&#x6790;&#x6784;&#x51FD;&#x6570;&#x8C03;&#x7528;wait_and_dispatch()&#x51FD;&#x6570;&#xFF0C;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;&#x5FAA;&#x73AF;&#x2460;&#xFF0C;&#x7B49;&#x5F85;&#x6D88;&#x606F;&#x7684;&#x4F20;&#x5165;(&#x8FD9;&#x6837;&#x624D;&#x80FD;&#x8FDB;&#x884C;&#x5F39;&#x51FA;&#x64CD;&#x4F5C;)&#xFF0C;&#x7136;&#x540E;&#x5C06;&#x6D88;&#x606F;&#x4F20;&#x9012;&#x7ED9;dispatch()&#x51FD;&#x6570;&#x3002;dispatch()&#x51FD;&#x6570;&#x672C;&#x8EAB;&#x2461;&#x5F88;&#x7B80;&#x5355;&#xFF0C;&#x4F1A;&#x68C0;&#x67E5;&#x5C0F;&#x65F6;&#x662F;&#x5426;&#x662F;&#x4E00;&#x4E2A;close_queue&#x6D88;&#x606F;&#xFF0C;&#x5F53;&#x662F;close_queue&#x6D88;&#x606F;&#x65F6;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x5F02;&#x5E38;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x662F;&#xFF0C;&#x51FD;&#x6570;&#x5C06;&#x4F1A;&#x8FD4;&#x56DE;false&#x6765;&#x8868;&#x660E;&#x6D88;&#x606F;&#x6CA1;&#x6709;&#x88AB;&#x5904;&#x7406;&#x3002;&#x56E0;&#x4E3A;&#x4F1A;&#x629B;&#x51FA;close_queue&#x5F02;&#x5E38;&#xFF0C;&#x6240;&#x4EE5;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4F1A;&#x6807;&#x793A;&#x4E3A;<code>noexcept(false)</code>&#x3002;&#x5728;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x6807;&#x8BC6;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6790;&#x6784;&#x51FD;&#x6570;&#x90FD;&#x4E3A;<code>noexcept(true)</code>&#x2463;&#x578B;&#xFF0C;&#x8FD9;&#x8868;&#x793A;&#x6CA1;&#x6709;&#x4EFB;&#x4F55;&#x5F02;&#x5E38;&#x629B;&#x51FA;&#xFF0C;&#x5E76;&#x4E14;close_queue&#x5F02;&#x5E38;&#x5C06;&#x4F1A;&#x4F7F;&#x7A0B;&#x5E8F;&#x7EC8;&#x6B62;&#x3002;</p>
<p>&#x867D;&#x7136;&#x4E0D;&#x4F1A;&#x7ECF;&#x5E38;&#x7684;&#x53BB;&#x8C03;&#x7528;wait()&#x51FD;&#x6570;&#xFF0C;&#x4F46;&#x5728;&#x5927;&#x591A;&#x6570;&#x65F6;&#x95F4;&#x91CC;&#xFF0C;&#x90FD;&#x5E0C;&#x671B;&#x5BF9;&#x4E00;&#x6761;&#x6D88;&#x606F;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x3002;&#x8FD9;&#x65F6;&#x5C31;&#x9700;&#x8981;handle()&#x6210;&#x5458;&#x51FD;&#x6570;&#x2462;&#x7684;&#x52A0;&#x5165;&#x3002;&#x8FD9;&#x4E2A;&#x51FD;&#x6570;&#x662F;&#x4E00;&#x4E2A;&#x6A21;&#x677F;&#xFF0C;&#x5E76;&#x4E14;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x4E0D;&#x53EF;&#x63A8;&#x65AD;&#xFF0C;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x6307;&#x5B9A;&#x9700;&#x8981;&#x5904;&#x7406;&#x7684;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#xFF0C;&#x5E76;&#x4E14;&#x4F20;&#x5165;&#x51FD;&#x6570;(&#x6216;&#x53EF;&#x8C03;&#x7528;&#x5BF9;&#x8C61;)&#x8FDB;&#x884C;&#x5904;&#x7406;&#xFF0C;&#x5E76;&#x5C06;&#x961F;&#x5217;&#x4F20;&#x5165;&#x5F53;&#x524D;dispatcher&#x5BF9;&#x8C61;&#x7684;handle()&#x51FD;&#x6570;&#x3002;&#x5728;&#x6D4B;&#x8BD5;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4E2D;&#x7684;chained&#x503C;&#x524D;&#x8981;&#x7B49;&#x5F85;&#x6D88;&#x606F;&#xFF0C;&#x4E0D;&#x4EC5;&#x662F;&#x907F;&#x514D;&#x201C;&#x79FB;&#x52A8;&#x201D;&#x7C7B;&#x578B;&#x7684;&#x5BF9;&#x8C61;&#x5BF9;&#x6D88;&#x606F;&#x8FDB;&#x884C;&#x7B49;&#x5F85;&#xFF0C;&#x800C;&#x4E14;&#x5141;&#x8BB8;&#x5C06;&#x7B49;&#x5F85;&#x72B6;&#x6001;&#x8F6C;&#x79FB;&#x5230;&#x65B0;&#x7684;TemplateDispatcher&#x5B9E;&#x4F8B;&#x4E2D;&#x3002;</p>
<p>&#x4EE3;&#x7801;C.5 TemplateDispatcher&#x7C7B;&#x6A21;&#x677F;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">namespace</span> messaging
{
  <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> PreviousDispatcher,<span class="hljs-keyword">typename</span> Msg,<span class="hljs-keyword">typename</span> Func&gt;
  <span class="hljs-keyword">class</span> TemplateDispatcher
  {
    <span class="hljs-built_in">queue</span>* q;
    PreviousDispatcher* prev;
    Func f;
    <span class="hljs-keyword">bool</span> chained;

    TemplateDispatcher(TemplateDispatcher <span class="hljs-keyword">const</span>&amp;)=<span class="hljs-keyword">delete</span>;
    TemplateDispatcher&amp; <span class="hljs-keyword">operator</span>=(TemplateDispatcher <span class="hljs-keyword">const</span>&amp;)=<span class="hljs-keyword">delete</span>;

    <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> Dispatcher,<span class="hljs-keyword">typename</span> OtherMsg,<span class="hljs-keyword">typename</span> OtherFunc&gt;
    <span class="hljs-keyword">friend</span> <span class="hljs-keyword">class</span> TemplateDispatcher;  <span class="hljs-comment">// &#x6240;&#x6709;&#x7279;&#x5316;&#x7684;TemplateDispatcher&#x7C7B;&#x578B;&#x5B9E;&#x4F8B;&#x90FD;&#x662F;&#x53CB;&#x5143;&#x7C7B;</span>

    <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">wait_and_dispatch</span><span class="hljs-params">()</span>
    </span>{
      <span class="hljs-keyword">for</span>(;;)
      {
        <span class="hljs-keyword">auto</span> msg=q-&gt;wait_and_pop();
        <span class="hljs-keyword">if</span>(dispatch(msg))  <span class="hljs-comment">// 1 &#x5982;&#x679C;&#x6D88;&#x606F;&#x5904;&#x7406;&#x8FC7;&#x540E;&#xFF0C;&#x4F1A;&#x8DF3;&#x51FA;&#x5FAA;&#x73AF;</span>
          <span class="hljs-keyword">break</span>;
      }
    }

    <span class="hljs-function"><span class="hljs-keyword">bool</span> <span class="hljs-title">dispatch</span><span class="hljs-params">(<span class="hljs-built_in">std</span>::<span class="hljs-built_in">shared_ptr</span>&lt;message_base&gt; <span class="hljs-keyword">const</span>&amp; msg)</span>
    </span>{
      <span class="hljs-keyword">if</span>(wrapped_message&lt;Msg&gt;* wrapper=
         <span class="hljs-keyword">dynamic_cast</span>&lt;wrapped_message&lt;Msg&gt;*&gt;(msg.get()))  <span class="hljs-comment">// 2 &#x68C0;&#x67E5;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#xFF0C;&#x5E76;&#x4E14;&#x8C03;&#x7528;&#x51FD;&#x6570;</span>
      {
        f(wrapper-&gt;contents);
        <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>;
      }
      <span class="hljs-keyword">else</span>
      {
        <span class="hljs-keyword">return</span> prev-&gt;dispatch(msg);  <span class="hljs-comment">// 3 &#x94FE;&#x63A5;&#x5230;&#x4E4B;&#x524D;&#x7684;&#x8C03;&#x5EA6;&#x5668;&#x4E0A;</span>
      }
    }
  <span class="hljs-keyword">public</span>:
    TemplateDispatcher(TemplateDispatcher&amp;&amp; other):
        q(other.q),prev(other.prev),f(<span class="hljs-built_in">std</span>::move(other.f)),
        chained(other.chained)
    {
      other.chained=<span class="hljs-literal">true</span>;
    }
    TemplateDispatcher(<span class="hljs-built_in">queue</span>* q_,PreviousDispatcher* prev_,Func&amp;&amp; f_):
        q(q_),prev(prev_),f(<span class="hljs-built_in">std</span>::forward&lt;Func&gt;(f_)),chained(<span class="hljs-literal">false</span>)
    {
      prev_-&gt;chained=<span class="hljs-literal">true</span>;
    }

    <span class="hljs-keyword">template</span>&lt;<span class="hljs-keyword">typename</span> OtherMsg,<span class="hljs-keyword">typename</span> OtherFunc&gt;
    TemplateDispatcher&lt;TemplateDispatcher,OtherMsg,OtherFunc&gt;
    handle(OtherFunc&amp;&amp; of)  <span class="hljs-comment">// 4 &#x53EF;&#x4EE5;&#x94FE;&#x63A5;&#x5176;&#x4ED6;&#x5904;&#x7406;&#x5668;</span>
    {
      <span class="hljs-keyword">return</span> TemplateDispatcher&lt;
          TemplateDispatcher,OtherMsg,OtherFunc&gt;(
          q,<span class="hljs-keyword">this</span>,<span class="hljs-built_in">std</span>::forward&lt;OtherFunc&gt;(of));
    }

    ~TemplateDispatcher() <span class="hljs-keyword">noexcept</span>(<span class="hljs-literal">false</span>)  <span class="hljs-comment">// 5 &#x8FD9;&#x4E2A;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4E5F;&#x662F;noexcept(false)&#x7684;</span>
    {
      <span class="hljs-keyword">if</span>(!chained)
      {
        wait_and_dispatch();
      }
    }
  };
}
</code></pre>
<p><code>TemplateDispatcher&lt;&gt;</code>&#x7C7B;&#x6A21;&#x677F;&#x4EFF;&#x7167;&#x4E86;dispatcher&#x7C7B;&#xFF0C;&#x4E8C;&#x8005;&#x51E0;&#x4E4E;&#x76F8;&#x540C;&#x3002;&#x7279;&#x522B;&#x662F;&#x6790;&#x6784;&#x51FD;&#x6570;&#x4E0A;&#xFF0C;&#x90FD;&#x662F;&#x8C03;&#x7528;wait_and_dispatch()&#x6765;&#x7B49;&#x5F85;&#x5904;&#x7406;&#x6D88;&#x606F;&#x3002;</p>
<p>&#x5904;&#x7406;&#x6D88;&#x606F;&#x7684;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x629B;&#x51FA;&#x5F02;&#x5E38;&#xFF0C;&#x5C31;&#x9700;&#x8981;&#x68C0;&#x67E5;&#x4E00;&#x4E0B;&#x5728;&#x5FAA;&#x73AF;&#x4E2D;&#x2460;&#x6D88;&#x606F;&#x662F;&#x5426;&#x5DF2;&#x7ECF;&#x5F97;&#x5230;&#x4E86;&#x5904;&#x7406;&#x3002;&#x5F53;&#x6210;&#x529F;&#x7684;&#x5904;&#x7406;&#x4E86;&#x4E00;&#x6761;&#x6D88;&#x606F;&#xFF0C;&#x5904;&#x7406;&#x8FC7;&#x7A0B;&#x5C31;&#x53EF;&#x4EE5;&#x505C;&#x6B62;&#x4E86;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x7B49;&#x5F85;&#x4E0B;&#x4E00;&#x7EC4;&#x6D88;&#x606F;&#x7684;&#x4F20;&#x5165;&#x4E86;&#x3002;&#x5F53;&#x83B7;&#x53D6;&#x4E86;&#x4E00;&#x4E2A;&#x548C;&#x6307;&#x5B9A;&#x7C7B;&#x578B;&#x5339;&#x914D;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x4F7F;&#x7528;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x65B9;&#x5F0F;&#x2461;&#x5C31;&#x8981;&#x597D;&#x4E8E;&#x629B;&#x51FA;&#x5F02;&#x5E38;(&#x5904;&#x7406;&#x51FD;&#x6570;&#x4E5F;&#x53EF;&#x80FD;&#x4F1A;&#x629B;&#x51FA;&#x5F02;&#x5E38;)&#x3002;&#x5982;&#x679C;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x4E0D;&#x5339;&#x914D;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x94FE;&#x63A5;&#x524D;&#x4E00;&#x4E2A;&#x8C03;&#x5EA6;&#x5668;&#x2462;&#x3002;&#x5728;&#x7B2C;&#x4E00;&#x4E2A;&#x5B9E;&#x4F8B;&#x4E2D;&#xFF0C;dispatcher&#x5B9E;&#x4F8B;&#x786E;&#x5B9E;&#x4F5C;&#x4E3A;&#x4E00;&#x4E2A;&#x8C03;&#x5EA6;&#x5668;&#xFF0C;&#x5F53;&#x5728;handle()&#x2463;&#x51FD;&#x6570;&#x4E2D;&#x8FDB;&#x884C;&#x94FE;&#x63A5;&#x540E;&#xFF0C;&#x5C31;&#x5141;&#x8BB8;&#x5904;&#x7406;&#x591A;&#x79CD;&#x7C7B;&#x578B;&#x7684;&#x6D88;&#x606F;&#x3002;&#x5728;&#x94FE;&#x63A5;&#x4E86;&#x4E4B;&#x524D;&#x7684;<code>TemplateDispatcher&lt;&gt;</code>&#x5B9E;&#x4F8B;&#x540E;&#xFF0C;&#x5F53;&#x6D88;&#x606F;&#x7C7B;&#x578B;&#x548C;&#x5F53;&#x524D;&#x7684;&#x8C03;&#x5EA6;&#x5668;&#x7C7B;&#x578B;&#x4E0D;&#x5339;&#x914D;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x8C03;&#x5EA6;&#x94FE;&#x4F1A;&#x4F9D;&#x6B21;&#x7684;&#x5411;&#x524D;&#x5BFB;&#x627E;&#x7C7B;&#x578B;&#x5339;&#x914D;&#x7684;&#x8C03;&#x5EA6;&#x5668;&#x3002;&#x56E0;&#x4E3A;&#x4EFB;&#x4F55;&#x8C03;&#x5EA6;&#x5668;&#x90FD;&#x53EF;&#x80FD;&#x629B;&#x51FA;&#x5F02;&#x5E38;(&#x5305;&#x62EC;dispatcher&#x4E2D;&#x5BF9;close_queue&#x6D88;&#x606F;&#x8FDB;&#x884C;&#x5904;&#x7406;&#x7684;&#x9ED8;&#x8BA4;&#x5904;&#x7406;&#x5668;)&#xFF0C;&#x6790;&#x6784;&#x51FD;&#x6570;&#x5728;&#x8FD9;&#x91CC;&#x4F1A;&#x518D;&#x6B21;&#x88AB;&#x58F0;&#x660E;&#x4E3A;<code>noexcept(false)</code>&#x2464;&#x3002;</p>
<p>&#x8FD9;&#x79CD;&#x7B80;&#x5355;&#x7684;&#x67B6;&#x6784;&#x5141;&#x8BB8;&#x4F60;&#x60F3;&#x961F;&#x5217;&#x63A8;&#x9001;&#x4EFB;&#x4F55;&#x7C7B;&#x578B;&#x7684;&#x6D88;&#x606F;&#xFF0C;&#x5E76;&#x4E14;&#x8C03;&#x5EA6;&#x5668;&#x6709;&#x9009;&#x62E9;&#x7684;&#x4E0E;&#x63A5;&#x6536;&#x7AEF;&#x7684;&#x6D88;&#x606F;&#x8FDB;&#x884C;&#x5339;&#x914D;&#x3002;&#x540C;&#x6837;&#xFF0C;&#x4E5F;&#x5141;&#x8BB8;&#x4E3A;&#x4E86;&#x63A8;&#x9001;&#x6D88;&#x606F;&#xFF0C;&#x5C06;&#x6D88;&#x606F;&#x961F;&#x5217;&#x7684;&#x5F15;&#x7528;&#x8FDB;&#x884C;&#x4F20;&#x9012;&#x7684;&#x540C;&#x65F6;&#xFF0C;&#x4FDD;&#x6301;&#x63A5;&#x6536;&#x7AEF;&#x7684;&#x79C1;&#x6709;&#x6027;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x5B8C;&#x6210;&#x7B2C;4&#x7AE0;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x6D88;&#x606F;&#x7684;&#x7EC4;&#x6210;&#x5C06;&#x5728;&#x6E05;&#x5355;C.6&#x4E2D;&#x7ED9;&#x51FA;&#xFF0C;&#x5404;&#x79CD;&#x72B6;&#x6001;&#x673A;&#x5C06;&#x5728;&#x4EE3;&#x7801;C.7,C.8&#x548C;C.9&#x4E2D;&#x7ED9;&#x51FA;&#x3002;&#x6700;&#x540E;&#xFF0C;&#x9A71;&#x52A8;&#x4EE3;&#x7801;&#x5C06;&#x5728;C.10&#x7ED9;&#x51FA;&#x3002;</p>
<p>&#x4EE3;&#x7801;C.6 ATM&#x6D88;&#x606F;</p>
<pre><code class="lang-c++">struct withdraw
{
  std::string account;
  unsigned amount;
  mutable messaging::sender atm_queue;

  withdraw(std::string const&amp; account_,
           unsigned amount_,
           messaging::sender atm_queue_):
    account(account_),amount(amount_),
    atm_queue(atm_queue_)
  {}
};

struct withdraw_ok
{};

struct withdraw_denied
{};

struct cancel_withdrawal
{
  std::string account;
  unsigned amount;
  cancel_withdrawal(std::string const&amp; account_,
                    unsigned amount_):
    account(account_),amount(amount_)
  {}
};

struct withdrawal_processed
{
  std::string account;
  unsigned amount;
  withdrawal_processed(std::string const&amp; account_,
                       unsigned amount_):
    account(account_),amount(amount_)
  {}
};

struct card_inserted
{
  std::string account;
  explicit card_inserted(std::string const&amp; account_):
    account(account_)
  {}
};

struct digit_pressed
{
  char digit;
  explicit digit_pressed(char digit_):
    digit(digit_)
  {}
};

struct clear_last_pressed
{};

struct eject_card
{};

struct withdraw_pressed
{
  unsigned amount;
  explicit withdraw_pressed(unsigned amount_):
    amount(amount_)
  {}
};

struct cancel_pressed
{};

struct issue_money
{
  unsigned amount;
  issue_money(unsigned amount_):
    amount(amount_)
  {}
};

struct verify_pin
{
  std::string account;
  std::string pin;
  mutable messaging::sender atm_queue;

  verify_pin(std::string const&amp; account_,std::string const&amp; pin_,
             messaging::sender atm_queue_):
    account(account_),pin(pin_),atm_queue(atm_queue_)
  {}
};

struct pin_verified
{};

struct pin_incorrect
{};

struct display_enter_pin
{};

struct display_enter_card
{};

struct display_insufficient_funds
{};

struct display_withdrawal_cancelled
{};

struct display_pin_incorrect_message
{};

struct display_withdrawal_options
{};

struct get_balance
{
  std::string account;
  mutable messaging::sender atm_queue;

  get_balance(std::string const&amp; account_,messaging::sender atm_queue_):
    account(account_),atm_queue(atm_queue_)
  {} 
};

struct balance
{
  unsigned amount;
  explicit balance(unsigned amount_):
    amount(amount_)
  {}
};

struct display_balance
{
  unsigned amount;
  explicit display_balance(unsigned amount_):
    amount(amount_)
  {}
};

struct balance_pressed
{};
</code></pre>
<p>&#x4EE3;&#x7801;C.7 ATM&#x72B6;&#x6001;&#x673A;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> atm
{
  messaging::receiver incoming;
  messaging::sender bank;
  messaging::sender interface_hardware;

  <span class="hljs-keyword">void</span> (atm::*state)();

  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> account;
  <span class="hljs-keyword">unsigned</span> withdrawal_amount;
  <span class="hljs-built_in">std</span>::<span class="hljs-built_in">string</span> pin;

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">process_withdrawal</span><span class="hljs-params">()</span> 
  </span>{
    incoming.wait()
      .handle&lt;withdraw_ok&gt;(
       [&amp;](withdraw_ok <span class="hljs-keyword">const</span>&amp; msg)
       {
         interface_hardware.send(
           issue_money(withdrawal_amount));

         bank.send(
           withdrawal_processed(account,withdrawal_amount));

         state=&amp;atm::done_processing;
       })
      .handle&lt;withdraw_denied&gt;(
       [&amp;](withdraw_denied <span class="hljs-keyword">const</span>&amp; msg)
       {
         interface_hardware.send(display_insufficient_funds());

         state=&amp;atm::done_processing;
       })
      .handle&lt;cancel_pressed&gt;(
       [&amp;](cancel_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         bank.send(
           cancel_withdrawal(account,withdrawal_amount));

         interface_hardware.send(
           display_withdrawal_cancelled());

         state=&amp;atm::done_processing;
       });
   }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">process_balance</span><span class="hljs-params">()</span>
  </span>{
    incoming.wait()
      .handle&lt;balance&gt;(
       [&amp;](balance <span class="hljs-keyword">const</span>&amp; msg)
       {
         interface_hardware.send(display_balance(msg.amount));

         state=&amp;atm::wait_for_action;
       })
      .handle&lt;cancel_pressed&gt;(
       [&amp;](cancel_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         state=&amp;atm::done_processing;
       });
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">wait_for_action</span><span class="hljs-params">()</span>
  </span>{
    interface_hardware.send(display_withdrawal_options());

    incoming.wait()
      .handle&lt;withdraw_pressed&gt;(
       [&amp;](withdraw_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         withdrawal_amount=msg.amount;
         bank.send(withdraw(account,msg.amount,incoming));
         state=&amp;atm::process_withdrawal;
       })
      .handle&lt;balance_pressed&gt;(
       [&amp;](balance_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         bank.send(get_balance(account,incoming));
         state=&amp;atm::process_balance;
       })
      .handle&lt;cancel_pressed&gt;(
       [&amp;](cancel_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         state=&amp;atm::done_processing;
       });
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">verifying_pin</span><span class="hljs-params">()</span>
  </span>{
    incoming.wait()
      .handle&lt;pin_verified&gt;(
       [&amp;](pin_verified <span class="hljs-keyword">const</span>&amp; msg)
       {
         state=&amp;atm::wait_for_action;
       })
      .handle&lt;pin_incorrect&gt;(
       [&amp;](pin_incorrect <span class="hljs-keyword">const</span>&amp; msg)
       {
         interface_hardware.send(
         display_pin_incorrect_message());
         state=&amp;atm::done_processing;
       })
      .handle&lt;cancel_pressed&gt;(
       [&amp;](cancel_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         state=&amp;atm::done_processing;
       });
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">getting_pin</span><span class="hljs-params">()</span>
  </span>{
    incoming.wait()
      .handle&lt;digit_pressed&gt;(
       [&amp;](digit_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         <span class="hljs-keyword">unsigned</span> <span class="hljs-keyword">const</span> pin_length=<span class="hljs-number">4</span>;
         pin+=msg.digit;

         <span class="hljs-keyword">if</span>(pin.length()==pin_length)
         {
           bank.send(verify_pin(account,pin,incoming));
           state=&amp;atm::verifying_pin;
         }
       })
      .handle&lt;clear_last_pressed&gt;(
       [&amp;](clear_last_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         <span class="hljs-keyword">if</span>(!pin.empty())
         {
           pin.pop_back();
         }
       })
      .handle&lt;cancel_pressed&gt;(
       [&amp;](cancel_pressed <span class="hljs-keyword">const</span>&amp; msg)
       {
         state=&amp;atm::done_processing;
       });
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">waiting_for_card</span><span class="hljs-params">()</span>
  </span>{
    interface_hardware.send(display_enter_card());

    incoming.wait()
      .handle&lt;card_inserted&gt;(
       [&amp;](card_inserted <span class="hljs-keyword">const</span>&amp; msg)
       {
         account=msg.account;
         pin=<span class="hljs-string">&quot;&quot;</span>;
         interface_hardware.send(display_enter_pin());
         state=&amp;atm::getting_pin;
       });
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">done_processing</span><span class="hljs-params">()</span>
  </span>{
    interface_hardware.send(eject_card());
    state=&amp;atm::waiting_for_card;
  }

  atm(atm <span class="hljs-keyword">const</span>&amp;)=<span class="hljs-keyword">delete</span>;
  atm&amp; <span class="hljs-keyword">operator</span>=(atm <span class="hljs-keyword">const</span>&amp;)=<span class="hljs-keyword">delete</span>;
<span class="hljs-keyword">public</span>:
  atm(messaging::sender bank_,
  messaging::sender interface_hardware_):
  bank(bank_),interface_hardware(interface_hardware_)
  {}

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">done</span><span class="hljs-params">()</span>
  </span>{
    get_sender().send(messaging::close_queue());
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span>
  </span>{
    state=&amp;atm::waiting_for_card;
    <span class="hljs-keyword">try</span>
    {
      <span class="hljs-keyword">for</span>(;;)
      {
        (<span class="hljs-keyword">this</span>-&gt;*state)();
      }
    }
    <span class="hljs-keyword">catch</span>(messaging::close_queue <span class="hljs-keyword">const</span>&amp;)
    {
    }
  }

  messaging::<span class="hljs-function">sender <span class="hljs-title">get_sender</span><span class="hljs-params">()</span>
  </span>{
    <span class="hljs-keyword">return</span> incoming;
  } 
};
</code></pre>
<p>&#x4EE3;&#x7801;C.8 &#x94F6;&#x884C;&#x72B6;&#x6001;&#x673A;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> bank_machine
{
  messaging::receiver incoming;
  <span class="hljs-keyword">unsigned</span> balance;
<span class="hljs-keyword">public</span>:
  bank_machine():

  balance(<span class="hljs-number">199</span>)
  {}

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">done</span><span class="hljs-params">()</span>
  </span>{
    get_sender().send(messaging::close_queue());
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span>
  </span>{
    <span class="hljs-keyword">try</span>
    {
      <span class="hljs-keyword">for</span>(;;)
      {
        incoming.wait()
          .handle&lt;verify_pin&gt;(
           [&amp;](verify_pin <span class="hljs-keyword">const</span>&amp; msg)
           {
             <span class="hljs-keyword">if</span>(msg.pin==<span class="hljs-string">&quot;1937&quot;</span>)
             {
               msg.atm_queue.send(pin_verified());
             }
             <span class="hljs-keyword">else</span>
             {
               msg.atm_queue.send(pin_incorrect());
             }
           })
          .handle&lt;withdraw&gt;(
           [&amp;](withdraw <span class="hljs-keyword">const</span>&amp; msg)
           {
             <span class="hljs-keyword">if</span>(balance&gt;=msg.amount)
             {
               msg.atm_queue.send(withdraw_ok());
               balance-=msg.amount;
             }
             <span class="hljs-keyword">else</span>
             {
               msg.atm_queue.send(withdraw_denied());
             }
           })
          .handle&lt;get_balance&gt;(
           [&amp;](get_balance <span class="hljs-keyword">const</span>&amp; msg)
           {
             msg.atm_queue.send(::balance(balance));
           })
          .handle&lt;withdrawal_processed&gt;(
           [&amp;](withdrawal_processed <span class="hljs-keyword">const</span>&amp; msg)
           {
           })
          .handle&lt;cancel_withdrawal&gt;(
           [&amp;](cancel_withdrawal <span class="hljs-keyword">const</span>&amp; msg)
           {
           });
      }
    }
    <span class="hljs-keyword">catch</span>(messaging::close_queue <span class="hljs-keyword">const</span>&amp;)
    {
    }
  }

  messaging::<span class="hljs-function">sender <span class="hljs-title">get_sender</span><span class="hljs-params">()</span>
  </span>{
  <span class="hljs-keyword">return</span> incoming;
  }
};
</code></pre>
<p>&#x4EE3;&#x7801;C.9 &#x7528;&#x6237;&#x72B6;&#x6001;&#x673A;</p>
<pre><code class="lang-c++"><span class="hljs-keyword">class</span> interface_machine
{
  messaging::receiver incoming;
<span class="hljs-keyword">public</span>:
  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">done</span><span class="hljs-params">()</span>
  </span>{
    get_sender().send(messaging::close_queue());
  }

  <span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">run</span><span class="hljs-params">()</span>
  </span>{
    <span class="hljs-keyword">try</span>
    {
      <span class="hljs-keyword">for</span>(;;)
      {
        incoming.wait()
          .handle&lt;issue_money&gt;(
           [&amp;](issue_money <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Issuing &quot;</span>
                 &lt;&lt;msg.amount&lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           })
          .handle&lt;display_insufficient_funds&gt;(
           [&amp;](display_insufficient_funds <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Insufficient funds&quot;</span>&lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           })
          .handle&lt;display_enter_pin&gt;(
           [&amp;](display_enter_pin <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Please enter your PIN (0-9)&quot;</span>&lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           })
          .handle&lt;display_enter_card&gt;(
           [&amp;](display_enter_card <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Please enter your card (I)&quot;</span>
                 &lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           })
          .handle&lt;display_balance&gt;(
           [&amp;](display_balance <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>
                 &lt;&lt;<span class="hljs-string">&quot;The balance of your account is &quot;</span>
                 &lt;&lt;msg.amount&lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           })
          .handle&lt;display_withdrawal_options&gt;(
           [&amp;](display_withdrawal_options <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Withdraw 50? (w)&quot;</span>&lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Display Balance? (b)&quot;</span>
                 &lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Cancel? (c)&quot;</span>&lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           })
          .handle&lt;display_withdrawal_cancelled&gt;(
           [&amp;](display_withdrawal_cancelled <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Withdrawal cancelled&quot;</span>
                 &lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           })
          .handle&lt;display_pin_incorrect_message&gt;(
           [&amp;](display_pin_incorrect_message <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;PIN incorrect&quot;</span>&lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           })
          .handle&lt;eject_card&gt;(
           [&amp;](eject_card <span class="hljs-keyword">const</span>&amp; msg)
           {
             {
               <span class="hljs-built_in">std</span>::lock_guard&lt;<span class="hljs-built_in">std</span>::mutex&gt; lk(iom);
               <span class="hljs-built_in">std</span>::<span class="hljs-built_in">cout</span>&lt;&lt;<span class="hljs-string">&quot;Ejecting card&quot;</span>&lt;&lt;<span class="hljs-built_in">std</span>::<span class="hljs-built_in">endl</span>;
             }
           });
      }
    }
    <span class="hljs-keyword">catch</span>(messaging::close_queue&amp;)
    {
    }
  }

  messaging::<span class="hljs-function">sender <span class="hljs-title">get_sender</span><span class="hljs-params">()</span>
  </span>{
    <span class="hljs-keyword">return</span> incoming;
  }
};
</code></pre>
<p>&#x4EE3;&#x7801;C.10 &#x9A71;&#x52A8;&#x4EE3;&#x7801;</p>
<pre><code class="lang-c++">int main()
{
  bank_machine bank;
  interface_machine interface_hardware;

  atm machine(bank.get_sender(),interface_hardware.get_sender());

  std::thread bank_thread(&amp;bank_machine::run,&amp;bank);
  std::thread if_thread(&amp;interface_machine::run,&amp;interface_hardware);
  std::thread atm_thread(&amp;atm::run,&amp;machine);

  messaging::sender atmqueue(machine.get_sender());

  bool quit_pressed=false;

  while(!quit_pressed)
  {
    char c=getchar();
    switch(c)
    {
    case &apos;0&apos;:
    case &apos;1&apos;:
    case &apos;2&apos;:
    case &apos;3&apos;:
    case &apos;4&apos;:
    case &apos;5&apos;:
    case &apos;6&apos;:
    case &apos;7&apos;:
    case &apos;8&apos;:
    case &apos;9&apos;:
      atmqueue.send(digit_pressed(c));
      break;
    case &apos;b&apos;:
      atmqueue.send(balance_pressed());
      break;
    case &apos;w&apos;:
      atmqueue.send(withdraw_pressed(50));
      break;
    case &apos;c&apos;:
      atmqueue.send(cancel_pressed());
      break;
    case &apos;q&apos;:
      quit_pressed=true;
      break;
    case &apos;i&apos;:
      atmqueue.send(card_inserted(&quot;acc1234&quot;));
      break;
    }
  }

  bank.done();
  machine.done();
  interface_hardware.done();

  atm_thread.join();
  bank_thread.join();
  if_thread.join();
}
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="../appendix_B/B.0-chinese.html" class="navigation navigation-prev " aria-label="Previous page: 附录B 并发库简要对比">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="../appendix_D/D.0-chinese.html" class="navigation navigation-next " aria-label="Next page: 附录D C++线程类库参考">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"附录C 消息传递框架与完整的ATM示例","level":"1.21","depth":1,"next":{"title":"附录D C++线程类库参考","level":"1.22","depth":1,"path":"content/appendix_D/D.0-chinese.md","ref":"content/appendix_D/D.0-chinese.md","articles":[{"title":"D.1 chrono头文件","level":"1.22.1","depth":2,"path":"content/appendix_D/D.1-chinese.md","ref":"content/appendix_D/D.1-chinese.md","articles":[]},{"title":"D.2 condition_variable头文件","level":"1.22.2","depth":2,"path":"content/appendix_D/D.2-chinese.md","ref":"content/appendix_D/D.2-chinese.md","articles":[]},{"title":"D.3 atomic头文件","level":"1.22.3","depth":2,"path":"content/appendix_D/D.3-chinese.md","ref":"content/appendix_D/D.3-chinese.md","articles":[]},{"title":"D.4 future头文件","level":"1.22.4","depth":2,"path":"content/appendix_D/D.4-chinese.md","ref":"content/appendix_D/D.4-chinese.md","articles":[]},{"title":"D.5 mutex头文件","level":"1.22.5","depth":2,"path":"content/appendix_D/D.5-chinese.md","ref":"content/appendix_D/D.5-chinese.md","articles":[]},{"title":"D.6 ratio头文件","level":"1.22.6","depth":2,"path":"content/appendix_D/D.6-chinese.md","ref":"content/appendix_D/D.6-chinese.md","articles":[]},{"title":"D.7 thread头文件","level":"1.22.7","depth":2,"path":"content/appendix_D/D.7-chinese.md","ref":"content/appendix_D/D.7-chinese.md","articles":[]}]},"previous":{"title":"附录B 并发库简要对比","level":"1.20","depth":1,"path":"content/appendix_B/B.0-chinese.md","ref":"content/appendix_B/B.0-chinese.md","articles":[]},"dir":"ltr"},"config":{"plugins":["livereload"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"plugins":["katex","ace"],"livereload":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"fontsettings":{"theme":"white","family":"sans","size":2},"fontSettings":{"theme":"white","family":"msyh","size":2},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"陈晓伟 chenxiaowei","pdf":{"pageNumbers":false,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":10,"left":10,"top":10,"bottom":20}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"C++并发编程实战（第2版）","language":"zh-hans","gitbook":"*","description":"《C++ Concurrency In Action - SECOND EDITION》英文版的中文翻译版。","direction":"ltr"},"file":{"path":"content/appendix_C/C.0-chinese.md","mtime":"2022-11-19T02:54:31.284Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2022-11-19T07:50:48.148Z"},"basePath":"../..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../../gitbook/gitbook.js"></script>
    <script src="../../gitbook/theme.js"></script>
    
        
        <script src="../../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

